using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Presentation;
namespace IGlobalist.Cms
{
    public partial class AddGallery : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                #region Add
                
                if((Request["pageID"]!=null) && (Request["categoryID"] == null))
                {
                    IList<ICmsClass> cList = ClassController.GetClassListByClassTypeID((short)CmsObjectType.ContentCategory);
                    ICmsClass classMedia = new CmsClass();
                    foreach (ICmsClass clazz in cList)
                    {
                        for (int i = 0; i < clazz.PropertyList.Count; i++)
                            if ((Convert.ToString(clazz.PropertyList[i].Name) == "Type") && (Convert.ToString(clazz.PropertyList[i].Value) == "Media"))
                            {
                                classMedia = clazz;
                                break;
                            }
                    }

                    long categoryID = 0;
                    if (classMedia.ID>0)
                    {
                        IContentCategory category = new Category();
                        if (ViewState["Category"] != null)
                        {
                            category = (Category)ViewState["Category"];
                            category.EditedBy = IGlobalistMembership.GetUser().ID;
                            categoryID = category.ID;
                        }
                        category.ExtPropertyList = classMedia.PropertyList;
                        category.Class = classMedia;
                        category.ClassID = classMedia.ID;
                        category.ClassTypeID = Convert.ToInt16(CmsObjectType.ContentCategory);
                        category.Name = Request["tbDescription"];
                        category.ParentID = 0;
                        category.Order = 0;// Convert.ToInt16(tbOrder.Text);
                        category.Description = Request["tbDescription"];
                        category.Author =IGlobalistMembership.GetUser().ID;


                        if (ViewState["Category"] != null)
                            CategoryController.UpdateCategory(category);
                        else
                        {
                            category.CreatedDate = DateTime.Now;
                            categoryID = CategoryController.InsertCategory(category);
                        }

                        //Add category in page
                        if (Convert.ToInt64(Request["pageID"]) > 0)
                            AddCategoryInPage(category, Convert.ToInt64(Request["pageID"]));

                    }
                }
                #endregion

                #region delete
                if (Request["categoryID"] != null)
                   {
                       IContentCategory category = CategoryController.GetCategoryNode(Convert.ToInt64(Request["categoryID"]));
                       foreach (IContentCategory cat in category.CategoryList)
                       {
                           CategoryController.DeleteCategory(cat.ID);
                           //delete image
                           try
                           {
                               string path = Path.Combine(DataRootPhysicalPath, "Photos/" + cat.ID + ".jpg");
                               FileInfo file = new FileInfo(path);
                               file.Delete();
                           }
                           catch { }
                       }

                       CategoryController.DeleteCategory(Convert.ToInt64(Request["categoryID"]));
                    //remover category in page

                       IPage page = PageController.GetPageCurrent(Convert.ToInt64(Request["pageID"]));
                       ContentCategoryList listCat = page.CategoryList;
                       foreach (IContentCategory c in listCat)
                           if (c.ID == Convert.ToInt64(Request["categoryID"]))
                           {
                               listCat.Remove(c);
                               break;
                           }
                       PageController.UpdatePage(page);

                   }    
                #endregion

                string url = Request.UrlReferrer.PathAndQuery;
                Response.Redirect(url);
            }
        }


        protected void AddCategoryInPage(IContentCategory category, long pageID)
        {
            IPage page = PageController.GetPageCurrent(pageID);
            ContentCategoryList list = page.CategoryList;
            list.Add(category);
            PageController.UpdatePage(page);
           
        }
    }
}
